package com.geo.geotool.service.geofile.fileCharset;

import com.geo.geotool.util.file.FileUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.File;

/**
 * • @Description: shape文件便编码格式捕获
 * • @author  : yell
 * • @Desc : shape文件编码格式查看，先确认是否包含 .cpg文件，如果包含.cpg文件，
 *           则直接读取cpg文件内容。如果不包含cpg文件，则需要读取.dbf文件中的第30位字节，该字节表示编码格式。
 * • @date : 2021/12/10  11:47
 */
public class ShapeFileCharsetCapture implements IFileCharsetCapture{
    /**编码格式字节位置*/
    final int INDEX_CHARSET = 30;
    @Override
    public String getCharsetName(String filePath) throws Exception{
        /**如果有cpg文件，则读取.cpg文件*/
        File shapeFile = new File(filePath);
        String parentPath = shapeFile.getParentFile().getPath();
        String shapeName = shapeFile.getName().substring(0,shapeFile.getName().indexOf("."));
        File shapeCpgFile = new File(parentPath+File.separator+shapeName+".cpg");
        if(shapeCpgFile.exists() && StringUtils.isNotEmpty(FileUtils.readFileToString(shapeCpgFile))){
            return FileUtils.readFileToString(shapeCpgFile);
        }
        /**读取.dbf文件**/
        String dbfPath = parentPath+File.separator+shapeName+".dbf";
        return getCharsetNameByDbfFile(dbfPath);
    }

    /***
     * 根据dbf文件获取编码格式
     * @param dbfFilePath
     * @return
     * @throws Exception
     */
    private String getCharsetNameByDbfFile(String dbfFilePath) throws Exception{
        byte[] dbfBytes = FileUtils.readFileToByteArray(new File(dbfFilePath));
        byte charsetByte = dbfBytes[INDEX_CHARSET-1];
        switch (charsetByte){
            case 0x01:
            case 0x09:
            case 0x0B:
            case 0x0D:
            case 0x0F:
            case 0x11:
            case 0x15:
            case 0x18:
            case 0x19:
            case 0x1B:
                return "IBM437";
            case 0x02:
            case 0x0A:
            case 0x0E:
            case 0x10:
            case 0x12:
            case 0x14:
            case 0x16:
            case 0x1A:
            case 0x1D:
            case 0x25:
            case 0x37:
                return "ibm850";
            case 0x03:
            case 0x58:
            case 0x59:
                return "Windows-1252";
            case 0x04:
                return "macintosh";
            case 0x08:
            case 0x17:
            case 0x66:
                return "IBM865";
            case 0x13:
            case 0x7B:
                return "iso-2022-jp";
            case 0x1C:
            case 0x6C:
                return "IBM863";
            case 0x1F:
            case 0x22:
            case 0x23:
            case 0x40:
            case 0x64:
            case (byte) 0x87:
                return "ibm852";
            case 0x24:
                return "IBM860";
            case 0x26:
            case 0x65:
                return "cp866";
            case 0x4D:
            case 0x7A:
                return "gb2312";
            case 0x4E:
            case 0x79:
                return "ks_c_5601-1987";
            case 0x4F:
            case 0x78:
                return "big5";
            case 0x50:
            case 0x7C:
                return "windows-874";
            case 0x57:
                return "GBK";
            case 0x67:
                return "ibm861";
            case 0x6A:
            case (byte) 0x86:
                return "ibm737";
            case 0x6B:
            case (byte) 0x88:
                return "ibm857";
            case (byte) 0x96:
                return "x-mac-cyrillic";
            case (byte) 0x97:
                return "x-mac-ce";
            case (byte) 0x98:
                return "x-mac-greek";
            case (byte) 0xC8:
                return "windows-1250";
            case (byte) 0xC9:
                return "windows-1251";
            case (byte) 0xCA:
                return "windows-1254";
            case (byte) 0xCB:
                return "windows-1253";
            case (byte) 0xCC:
                return "windows-1257";
            default:
                return "UTF-8";
        }
    }
}
